import cn.edu.dgut.infomation.LocalUserInfo;
import cn.edu.dgut.utils.ReadFile;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/checkInfo")
public class CheckInfoServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        int exsit = 0;
        String phone_number = request.getParameter("phone_number");
        LocalUserInfo[] users = new ReadFile().getUers();
        for (int i = 0; i < users.length; i++) {
            if(users[i].getPhone_number().equals(phone_number)){
                exsit = 1;
                break;
            }
        }

        String email = request.getParameter("email");
        for (int i = 0; i < users.length; i++) {
            if(users[i].getEmail().equals(email)){
                exsit = 1;
                break;
            }
        }

        String name = request.getParameter("name");
        for (int i = 0; i < users.length; i++) {
            if(users[i].getName().equals(name)){
                exsit = 1;
                break;
            }
        }

        response.setContentType("application/json,charset=utf-8");
        String strJson = "{\"exsit\":\""+exsit+"\"}";
        response.getWriter().println(strJson);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request,response);
    }
}
